<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true">
      <el-form-item label="盘点单编码" prop="inventoryNum">
        <el-input
          v-model="queryParams.inventoryNum"
          placeholder="请输入"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="盘点仓库" prop="machineName">
        <el-input
          v-model="queryParams.machineName"
          placeholder="请输入"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="盘点月份" prop="inventoryMonth">
        <el-date-picker
         style="width: 200px"
          v-model="queryParams.inventoryMonth"
          type="month"
          placeholder="请选择"
          format="YYYY-MM"
          value-format="YYYY-MM"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
                <el-button type="primary" plain icon="Upload" @click="handleImport">导入</el-button>
            </el-col> -->
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Download" @click="handleExport"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      border
      :data="inventorydata"
    >
      <el-table-column label="序号" width="60" type="index" align="center">
      </el-table-column>
      <el-table-column
        label="状态"
        align="center"
        prop="status"
        :show-overflow-tooltip="true"
      >
        <template #default="scope">
          <dict-tag
            :options="ems_inventorysheet_status"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="盘点单编码"
        width="200"
        align="center"
        prop="inventoryNum"
        :show-overflow-tooltip="true"
      >
        <template v-slot="{ row }">
          <el-button type="primary" @click="handleView(row)" text>{{
            row.inventoryNum
          }}</el-button>
        </template>
      </el-table-column>

      <el-table-column
        label="盘点月份"
        align="center"
        prop="inventoryMonth"
        :show-overflow-tooltip="true"
      />
      <!-- <el-table-column
        label="盘点日期"
        align="center"
        prop="createTime"
        :show-overflow-tooltip="true"
      /> -->
      <el-table-column
        label="盘点仓库"
        align="center"
        prop="machineName"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        label="盘点人"
        align="center"
        prop="inventoryUser"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="操作"
        align="center"
        fixed="right"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-tooltip
            content="编辑"
            placement="top"
            v-if="scope.row.status == 1"
          >
            <el-button
              link
              type="primary"
              icon="Edit"
              @click="handleEdit(scope.row)"
            ></el-button>
          </el-tooltip>
          <el-tooltip
            content="删除"
            placement="top"
            v-if="scope.row.status == 1"
          >
            <el-button
              link
              type="primary"
              icon="Delete"
              @click="handleDel(scope.row)"
            ></el-button>
          </el-tooltip>
          <el-button
            link
            type="primary"
            v-if="scope.row.status == 1"
            @click="handleService(scope.row)"
            >确认</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      style="margin-top: 0; padding-top: 0"
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>
 
<script setup name="supplier">
import { downLoadBlob } from "@/utils/ruoyi";
import useTagsViewStore from "@/store/modules/tagsView";
const tagsViewStore = useTagsViewStore();
import {
  inventoryList,
  updateInventory,
  delInventory,
  exportInventory,
} from "@/api/storage/inventorySheet.js";
const { proxy } = getCurrentInstance();
const { ems_inventorysheet_status } = proxy.useDict(
  "ems_inventorysheet_status"
);
const router = useRouter();
const route = useRoute();
const inventorydata = ref([]);
const loading = ref(false);
const total = ref(0);
import useUserStore from "@/store/modules/user";
const userInfo = useUserStore();
import { ElMessage, ElMessageBox } from "element-plus";
const showSearch = ref(true);
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
});
const inventory_store = [
  { label: "仓库", type: "0" },
  { label: "机台", type: "1" },
];
// 新增
const handleAdd = () => {
  router.push("/storage/inventorySheet/details");
};
const handleEdit = (row) => {
  router.push({
    path: "/storage/inventorySheet/details",
    query: {
      id: row.id,
      status: row.status,
      code: row.supplierCode,
    },
  });
};
// 查看
const handleView = (row) => {
  router.push({
    path: "/storage/inventorySheet/details",
    query: {
      id: row.id,
      readonly: true,
      code: row.supplierCode,
      status: row.status,
    },
  });
};
const handleDel = (row) => {
  ElMessageBox.confirm(
    "确定要删除编码为为 " + row.inventoryNum + " 的盘点单吗?",
    "提示",
    {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }
  )
    .then(() => {
      // 用户点击了确定按钮
      delInventory(row.id).then((response) => {
        if (response.code == 200) {
          ElMessage({
            message: "删除成功",
            type: "success",
          });
          getList();
        }
      });
    })
    .catch(() => {
      //取消
    });
};
function getCurrentTime() {
  const now = new Date();
  const year = now.getFullYear();
  const month = String(now.getMonth() + 1).padStart(2, "0");
  const day = String(now.getDate()).padStart(2, "0");
  const hours = String(now.getHours()).padStart(2, "0");
  const minutes = String(now.getMinutes()).padStart(2, "0");
  const seconds = String(now.getSeconds()).padStart(2, "0");
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}
//提交
const handleService = (row) => {
  ElMessageBox.confirm(
    `确定要确认 编码为 ${row.inventoryNum} 的盘点单吗?`,
    "提示",
    {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }
  ).then(() => {
    row.confirmUser = userInfo.name;
    row.confirmDate = getCurrentTime();
    // 用户点击了确定按钮
    const formData = { ...row };
    formData.status = 2;
    updateInventory(formData).then((response) => {
      if (response.code === 200) {
        ElMessage({
          message: "提交成功",
          type: "success",
        });
        getList();
      }
    });
  });
};
function getList() {
  loading.value = true;
  inventoryList(queryParams.value)
    .then((response) => {
      inventorydata.value = response.rows;
      total.value = response.total;
      loading.value = false;
    })
    .catch((err) => {
      loading.value = false;
    });
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
// 导出
const handleExport = () => {
  exportInventory(queryParams.value).then((res) => {
    downLoadBlob(res, "盘点单列表");
  });
};
getList();
</script>
